#include <string>
using namespace std;
class Solution
{
public:
    string compressString(string S)
    {
        if (S.empty())
            return "";
        string ans;
        char now = S[0];
        int count = 1, sz = S.size();
        for (int i = 1; i < sz; ++i)
        {
            if (S[i] == now)
                ++count;
            else
            {
                // 压入答案数组
                ans += now, ans += to_string(count);
                count = 1;
                now = S[i];
            }
        }
        // 把最后一个数也要算上
        ans += now, ans += to_string(count);
        if (ans.size() >= S.size())
            return S;
        return ans;
    }
};